package models;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

public class TurnManager
{
    public int     counter;
    public int     offset;
    public int     shift;
    List<String>   players;
    public boolean atEnd;
    public int     size;
    public String  turn;
    List<String>   folders;

    public TurnManager()
    {
        this.counter = 0;
        this.offset = 0;
        this.players = new ArrayList<String>();
        this.atEnd = false;
        this.size = 0;
        this.turn = "";
        this.shift = 0;
        this.folders = new ArrayList<String>();
    }

    public void addPlayer(String uuid)
    {
        this.players.add(uuid);
        ++this.size;
    }

    public void rmPlayer(String uuid)
    {
        // if uuid not in list...
        int index = this.players.indexOf(uuid);
        this.players.set(index, null);
        this.folders.remove(uuid);
        --this.size;
        if (index < ((this.shift + this.offset) % this.players.size()))
        {
            ++this.counter;
        }
    }

    public void addFolder(String uuid)
    {
        this.folders.add(uuid);
    }

    public void clearFolders()
    {
        this.folders.clear();
    }

    public void shuffle()
    {
        Collections.shuffle(this.players);
    }

    public boolean onePlayerLeft()
    {
        if (((this.players.size() - this.folders.size()) < 2))
        {
            return true;
        }
        return false;
    }

    public boolean atEnd()
    {
        if (((this.players.size() - this.folders.size()) < 2)
                && (this.players.size() > 2))
        {
            return true;
        }
        return this.atEnd;
    }

    public int playersCount()
    {
        return this.size;
    }

    public void shift()
    {
        ++this.shift;
    }

    public String nextTurn()
    {
        ListIterator<String> it = this.players
                .listIterator((this.shift + this.offset) % this.players.size());
        String uuid = null;
        this.atEnd = false;

        while (uuid == null)
        {
            if (it.hasNext())
            {
                uuid = it.next();
            }
            else
            {
                it = this.players.listIterator();
                uuid = it.next();
            }
            ++this.counter;
            ++this.offset;

            if (this.folders.contains(uuid))
            {
                uuid = null;
            }
            if (this.counter >= this.players.size())
            {
                this.atEnd = true;
                this.counter = 0;
                this.offset = 0;
            }
        }
        this.turn = uuid;

        return this.turn;
    }

    public boolean myTurn(String uuid)
    {
        return this.turn.equals(uuid);
    }

    public String current()
    {
        return this.turn;
    }

    public static void main(String[] args)
    {
        TurnManager tm = new TurnManager();
        tm.addPlayer("one");
        tm.addPlayer("two");
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        tm.shift();
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        tm.shift();
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        tm.shift();
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
        System.out.printf("%5s %b\n", tm.nextTurn(), tm.atEnd);
    }

}
